package com.weds.data_sync.controller;

import com.weds.data_sync.mapper.DataSyncRecordMapper;
import com.weds.data_sync.mapper.DataSyncScriptMapper;
import com.weds.data_sync.mapper.DataSyncScriptPlanMapper;
import com.weds.data_sync.pojo.dto.ScriptRequestDto;
import com.weds.data_sync.pojo.dto.ScriptResponseDto;
import com.weds.data_sync.pojo.entity.DataSyncRecordDO;
import com.weds.data_sync.service.ScriptService;
import com.weds.data_sync.service.impl.TestService;
import com.weds.data_sync.util.CommonUtil;
import com.weds.data_sync.util.JsonData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;

/**
 * @Description
 * @Author lihw
 * @Date 2024/1/3 8:39
 */
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private TestService testService;

    @Autowired
    private DataSyncScriptPlanMapper planMapper;

    @Autowired
    private DataSyncScriptMapper scriptMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    @GetMapping("enter")
    public void enter() {
        testService.start();
    }

    @Autowired
    private ScriptService scriptService;

    @Autowired
    private DataSyncRecordMapper recordMapper;

    @PostMapping("script_service")
    public ScriptResponseDto executorScript(@RequestBody ScriptRequestDto scriptRequestDto) throws InterruptedException {
        log.info("request: {}", scriptRequestDto);


        DataSyncRecordDO build1 = DataSyncRecordDO.builder()
                .tableName("user")
                .primaryId("1")
                .syncTime(LocalDateTime.now())
                .operateType(1)
                .oldData(null)
                .batchId(scriptRequestDto.getAckId())
                .build();
        DataSyncRecordDO build2 = DataSyncRecordDO.builder()
                .tableName("user")
                .primaryId("2")
                .syncTime(LocalDateTime.now())
                .operateType(1)
                .oldData(null)
                .batchId(scriptRequestDto.getAckId())
                .build();
        DataSyncRecordDO build3 = DataSyncRecordDO.builder()
                .tableName("user")
                .primaryId("3")
                .syncTime(LocalDateTime.now())
                .operateType(1)
                .oldData(null)
                .batchId(scriptRequestDto.getAckId())
                .build();

        DataSyncRecordDO build4 = DataSyncRecordDO.builder()
                .tableName("card")
                .primaryId("1")
                .syncTime(LocalDateTime.now())
                .operateType(1)
                .oldData(null)
                .batchId(scriptRequestDto.getAckId())
                .build();

        recordMapper.insert(build1);
        recordMapper.insert(build2);
        recordMapper.insert(build3);
        recordMapper.insert(build4);


        return ScriptResponseDto.builder()
                .code(0)
                .msg("成功")
                .askId(scriptRequestDto.getAckId())
                .data("succ")
                .build();
    }

    /**
     * 测试执行计划流程
     *
     * @return
     */
    @GetMapping("plan")
    public String testPlan() {
        // DataSyncScriptPlanDO dataSyncScriptPlanDO = planMapper.selectById(2);
        // List<DataSyncScriptPlanDO> dataSyncScriptPlanDOS = planMapper.selectList(null);
        // DataSyncScriptPlanDO dataSyncScriptPlanDO = dataSyncScriptPlanDOS.get(0);
        // List<DataSyncScriptDO> dataSyncScriptDOS = scriptMapper.selectBatchIds(dataSyncScriptPlanDO.getScriptArr());
        //
        // ExecutePlan executePlan = new ExecutePlanImpl(
        //         dataSyncScriptPlanDO,
        //         dataSyncScriptDOS,
        //         null,
        //         redisTemplate,
        //         applicationEventPublisher,
        //         scriptService
        // );
        //
        // Object result = executePlan.executeScript();
        // log.info("script result: {}", result);


        return "succ";
    }

    @GetMapping("code")
    public void getCodeImg(HttpServletRequest request, HttpServletResponse response) throws IOException {

        CommonUtil.getImageCaptcha(request, response, redisTemplate);

    }

    @GetMapping("check")
    public JsonData checkCodeImg(String code, HttpServletRequest request){
        Boolean b = CommonUtil.checkImageCaptcha(request, redisTemplate, code);
        if (b) {
            log.info("校验通过");
            return JsonData.buildSuccess();
        } else {
            log.info("校验失败");
            return JsonData.buildError("失败");
        }
    }
}
